﻿using HCQ2_Model;
using HCQ2_Model.APPModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HCQ2_BLL
{
    public partial class T_RoleBLL : HCQ2_IBLL.IT_RoleBLL
    {
        /// <summary>
        /// 获取所有的角色列表
        /// </summary>
        /// <returns></returns>
        public List<T_Role> GetRoleData(string roleName, int page, int rows,string sm_code)
        {
            List<T_Role> list = Select(s => true);
            if (!string.IsNullOrEmpty(roleName))
                list = list.Where(s => s.role_name.Contains(roleName))?.ToList();
            if (!string.IsNullOrEmpty(sm_code))
                list = list.Where(s => s.sm_code.Equals(sm_code))?.ToList();
            return list.OrderByDescending(s => s.update_date).Skip((page - 1) * rows).Take(rows)?.ToList();
        }
        /// <summary>
        ///  编辑角色实现
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool EditRole(T_Role role,int id)
        {
            if (role == null || id<=0)
                return false;
            int modify = Modify(role, s => s.role_id == id, "role_name", "role_code","role_note","sm_code", "default_role", "is_mark");
            //默认角色
            //if (role.default_role)
            //{
            //    List<T_UserRoleRelation> rel = DBSession.IT_UserRoleRelationDAL.Select(s => s.role_id == id);
            //    List<T_User> userList = DBSession.IT_UserDAL.Select(s => true);
            //    userList.ForEach(s => {
            //        int tempCount = rel.Where(o => o.user_id == s.user_id).Count();
            //        if(tempCount<=0)
            //            DBSession.IT_UserRoleRelationDAL.Add(new T_UserRoleRelation { user_id = s.user_id, role_id = id });
            //    });
            //}
            return true;
        }
        /// <summary>
        ///  添加角色实现
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool AddRole(T_Role role)
        {
            if (null == role)
                return false;
            int add = Add(role);
            if (add > 0)
            {
                ////判断是否为默认角色
                //if (role.default_role)
                //{
                //    //配置全部用户拥有此角色
                //    List<T_User> userList = DBSession.IT_UserDAL.Select(s => true);
                //    userList.ForEach(o => { DBSession.IT_UserRoleRelationDAL.Add(new T_UserRoleRelation { user_id = o.user_id, role_id = role.role_id }); });
                //}
                return true;
            }                
            return false;
        }
        /// <summary>
        ///  删除角色实现
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelRole(int id)
        {
            if (id <= 0)
                return false;
            //1.删除角色表
            int del = Delete(s => s.role_id == id);
            //2. 删除用户-角色表
            DBSession.IT_UserRoleRelationDAL.Delete(s => s.role_id == id);
            //3. 删除用户组-角色表
            DBSession.IT_RoleGroupRelationDAL.Delete(s => s.role_id == id);
            //4. 删除角色-权限表
            DBSession.IT_RolePermissRelationDAL.Delete(s => s.role_id == id);
            if (del > 0)
                return true;
            return false;
        }
        /// <summary>
        ///  获取角色数量
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="smCode"></param>
        /// <returns></returns>
        public int GetRoleCount(string roleName, string smCode)
        {
            return DBSession.IT_RoleDAL.GetRoleCount(roleName, smCode);
        }

        /// <summary>
        /// 获取待分配角色用户数据
        /// </summary>
        /// <param name="role_id"></param>
        /// <param name="unit_code"></param>
        /// <returns></returns>
        public List<HCQ2_Model.SelectModel.ListBoxModel> GetWaitRoleDataByPerson(int role_id, string unit_code = null)
        {
            if (role_id <= 0)
                return null;
            return DBSession.IT_RoleDAL.GetWaitRoleDataByPerson(role_id, unit_code);
        }
        /// <summary>
        ///  获取已分配角色用户数据
        /// </summary>
        /// <param name="role_id"></param>
        /// <returns></returns>
        public List<HCQ2_Model.SelectModel.ListBoxModel> GetFineRoleDataByPerson(int role_id)
        {
            if (role_id <= 0)
                return null;
            return DBSession.IT_RoleDAL.GetFineRoleDataByPerson(role_id);
        }
        /// <summary>
        ///  保存批量角色用户数据
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="role_id"></param>
        /// <returns></returns>
        public bool SaveRoleDataByPerson(string userid, int role_id)
        {
            if (role_id <= 0)
                return false;
            //判断是否清空用户
            if (string.IsNullOrEmpty(userid))
            {
                DBSession.IT_UserRoleRelationDAL.Delete(s => s.role_id == role_id);
                return true;
            }
            List<T_UserRoleRelation> roleRel = DBSession.IT_UserRoleRelationDAL.Select(s => s.role_id == role_id);
            string[] user = userid.Trim().Trim(',').Split(',');
            //循环添加角色
            foreach(var user_id in user)
            {
                int tempid = HCQ2_Common.Helper.ToInt(user_id);
                var query = roleRel.FindAll(o => o.user_id == tempid);
                if (query == null || query.Count <= 0)
                    DBSession.IT_UserRoleRelationDAL.Add(new T_UserRoleRelation { user_id = tempid, role_id = role_id });
            }
            //删除被排除的用户
            DBSession.IT_RoleDAL.DelBatchRole(role_id, userid);
            return true;
        }
        /// <summary>
        ///  获取用户拥有角色
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public List<RoleLimitModel> getUserRole(int user_id)
        {
            List<RoleLimitModel> result = new List<RoleLimitModel>();
            List<T_Role> list = DBSession.IT_PermissionsDAL.GetRolesById(user_id);
            list?.ForEach(o =>
            {
                result.Add(new RoleLimitModel { role_name = o.role_name, role_code = o.role_code, is_mark = o.is_mark });
            });
            return result;
        }
    }
}